﻿#include "SelectPickModelDialog.h"
#include "ui_SelectPickModelDialog.h"

#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

// Pick
#include "PickDataProvider/GUIPickInfo.h"
#include "PickDataProvider/PickedDataProvider.h"

#include <QButtonGroup>
#include <QRegExpValidator>
#include <QTabBar>

namespace GUI {

    SelectPickModelDialog::SelectPickModelDialog(Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUIDialogBase(parent),
        _oper(oper),
        _ui(new Ui::SelectPickModelDialog)
    {
        _ui->setupUi(this);
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //去掉问号
        Qt::WindowFlags flags = windowFlags();
        flags &= ~Qt::WindowContextHelpButtonHint;
        setWindowFlags(flags);

        //初始化参数
        _pickModelGroup = new QButtonGroup(this);
        _pickModelGroup->addButton(_ui->radioButton_BasicElem, 0);
        _pickModelGroup->addButton(_ui->radioButton_Pid, 1);
        _pickModelGroup->addButton(_ui->radioButton_Group, 2);
        connect(_pickModelGroup, SIGNAL(buttonClicked(int)), this, SLOT(pickModelGroup_buttonClicked(int)));
        _baseElementGroup = new QButtonGroup(this);
        _baseElementGroup->addButton(_ui->radioButton_Model1, 0);
        _baseElementGroup->addButton(_ui->radioButton_Model2, 1);
        _baseElementGroup->addButton(_ui->radioButton_Model3, 2);
        connect(_baseElementGroup, SIGNAL(buttonClicked(int)), this, SLOT(baseElementGroup_buttonClicked(int)));
        //去除tab的表头
        _ui->tabWidget_SelectPick->tabBar()->hide();
        _ui->lineEdit_NumLoop->setValidator(new QRegExpValidator(QRegExp("^[1-9]+([0-9])+$")));
        this->init();
        //获取拾取器，绑定拾取结束信号
        GraphData::PickedDataProvider* pickProvider = GraphData::PickedDataProvider::getInstance();
        //清除拾取对象所有的连接
        QObject::disconnect(pickProvider, SIGNAL(sig_pickedOver()), nullptr, nullptr);
        if (pickProvider)
            connect(pickProvider, &GraphData::PickedDataProvider::sig_pickedOver, this, &SelectPickModelDialog::pickFinished);
        //设置窗口自适应隐藏组件
        this->layout()->setSizeConstraint(QLayout::SetMinAndMaxSize);
    }

    SelectPickModelDialog::~SelectPickModelDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
        if (_pickModelGroup) delete _pickModelGroup;
        if (_baseElementGroup) delete _baseElementGroup;
    }

    void SelectPickModelDialog::pickModelGroup_buttonClicked(int id)
    {
        _ui->tabWidget_SelectPick->setCurrentIndex(id);
        if (id == 0)
            this->baseElementGroup_buttonClicked(_baseElementGroup->checkedId());
        else if (id == 1)
            this->meshPropPickModel();
        else if (id == 2)
            this->groupPickModel();
    }

    void SelectPickModelDialog::baseElementGroup_buttonClicked(int id)
    {
        _ui->label_NumLoop->setVisible(false);
        _ui->lineEdit_NumLoop->setVisible(false);
        _ui->groupBox->setVisible(false);
        if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell && id == 0)
        {
            //显示层数
            _ui->label_NumLoop->setVisible(true);
            _ui->lineEdit_NumLoop->setVisible(true);
        }
        else if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEABeam && (id == 1 || id == 2))
        {
            //显示方向点数据
            _ui->groupBox->setVisible(true);
        }
        GUI::GUIPickInfoStru pickInfo = GUI::GUIPickInfo::GetPickInfo();
        pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJNone;
        pickInfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMNone;
        //获取拾取类型
        if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate 
            || _subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell
            || _subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell)
        {
            pickInfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMIndividually;
            if (id == 0)
                pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJDiscNode;
            else if (id == 1)
                pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJGeometryFace;
            else if (id == 2)
                pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJMeshCell2D;
        }
        else if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEABeam)
        {
            pickInfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMIndividually;
            if (id == 0)
                pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJDiscNode;
            else if (id == 1)
                pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJGeometryEdge;
            else if (id == 2)
                pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJMeshCell1D;
        }
        else if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity)
        {
            //SubSystem下的Acoustic CAvities 创建声腔 
            pickInfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMIndividually;
            if (id == 0)
                pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJSEAShell;
            else if (id == 1)
                pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJGeometrySolid;//以几何体创建声腔
            else if (id == 2)
                pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJMeshCell3D;
        }
        //设置拾取方式
        GUI::GUIPickInfo::SetPickInfo(pickInfo);
    }

    void SelectPickModelDialog::meshPropPickModel()
    {
        GUI::GUIPickInfoStru pickInfo = GUI::GUIPickInfo::GetPickInfo();
        pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJNone;
        pickInfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMNone;
        if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate
            || _subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell
            || _subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell)
        {
            pickInfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMByMeshProp;
            pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJMeshCell2D;
        }
        //设置拾取方式
        GUI::GUIPickInfo::SetPickInfo(pickInfo);
    }

    void SelectPickModelDialog::groupPickModel()
    {

    }

    void SelectPickModelDialog::on_pushButton_Pick_click()
    {

    }

    void SelectPickModelDialog::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }

    void SelectPickModelDialog::on_pushButton_OK_clicked()
    {
        this->pickFinished();
    }

    void SelectPickModelDialog::reject()
    {
        //清除拾取模式和高亮
        GUI::GUIPickInfo::ClearPickInfo();
        this->QDialog::reject();
    }

    void SelectPickModelDialog::init()
    {
        //获取子系统类型
        if (!_oper)return;
        _oper->argValue<int>("SEASubSysType", _subSysType);
        if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate || _subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell
            || _subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell)
        {
            if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate)
                this->setWindowTitle(tr("Create Flat Plate SEA Subsystem"));
            else if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell)
                this->setWindowTitle(tr("Create Singly Curved Shell SEA Subsystem"));
            else if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell)
                this->setWindowTitle(tr("Create Doubly Curved Shell SEA Subsystem"));
            _ui->radioButton_Model1->setText(tr("Node"));
            _ui->radioButton_Model2->setText(tr("Geometry Surface"));
            _ui->radioButton_Model3->setText(tr("Mesh Face Cell"));
        }
        else if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEABeam)
        {
            this->setWindowTitle(tr("Create Beam SEA Subsystem"));
            _ui->radioButton_Model1->setText(tr("Node"));
            _ui->radioButton_Model2->setText(tr("Geometry Edge"));
            _ui->radioButton_Model3->setText(tr("Mesh Beam Cell"));
            _ui->radioButton_Pid->setEnabled(false);
            _ui->radioButton_Group->setEnabled(false);
        }
        else if (_subSysType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity)
        {
            this->setWindowTitle(tr("Create Cavity SEA Subsystem"));
            _ui->radioButton_Model1->setText(tr("Shell"));
            _ui->radioButton_Model2->setText(tr("Geometry Solid"));
            _ui->radioButton_Model3->setText(tr("Mesh Volume Cell"));
            _ui->radioButton_Pid->setEnabled(false);
            _ui->radioButton_Group->setEnabled(false);
            _ui->groupBox->setVisible(false);
        }
        //设置点拾取,并开启拾取模式
        _ui->radioButton_Model1->setChecked(true);
        baseElementGroup_buttonClicked(0);
    }

    void SelectPickModelDialog::pickFinished()
    {
        if (!_oper || !_pickModelGroup || !_baseElementGroup) return;
        int pickModelID = _pickModelGroup->checkedId();
        QString pickModel;
        if (pickModelID == 0)
        {
            QAbstractButton * btn = _baseElementGroup->checkedButton();
            if (btn)
                pickModel = btn->text();
        }
        else
        {
            QAbstractButton * btn = _pickModelGroup->checkedButton();
            if (btn)
                pickModel = btn->text();
        }
        _oper->setArgs("PickModel", pickModel);
        _oper->setArgs("NumLoop", _ui->lineEdit_NumLoop->text().toInt());
        this->accept();
    }
}
